package 字符串.替换空格;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/1/17 11:09
 * @description:
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        String str = "We are happy.";
        System.out.println(replaceSpace(str));
    }

    //构建stringBuilder
    public static String replaceSpace(String s) {
        StringBuilder sb = new StringBuilder();
        int start = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == ' '){
                sb.append(s.substring(start,i));
                sb.append("%20");
                start = i+1;
            }
        }
        sb.append(s.substring(start,s.length()));
        return String.valueOf(sb);
    }


    public static String replaceSpace2(String s) {
        StringBuilder string = new StringBuilder();
        for (int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if (c == ' '){
                string.append("%20");
            } else {
                string.append(c);
            }
        }
        return string.toString();
    }
}
